using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using System;
using System.Collections;
using yW2Lib;

namespace yW5Lib
{
	public class clsImportYW2
	{
		private clsProjectIO cProjectIO;

		public clsImportYW2()
		{
			cProjectIO = new clsProjectIO();
		}

		public bool ImportYW2(ref clsProject cProject, string YW2File = "")
		{
			//Discarded unreachable code: IL_0737, IL_074e, IL_0759, IL_0769, IL_08ef, IL_08f1, IL_0905, IL_0927
			int num = default(int);
			bool result = default(bool);
			int num5 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				clsYW2Project clsYW2Project = new clsYW2Project(ref clsShared.cLog);
				num2 = 3;
				string value = "";
				num2 = 4;
				if (Operators.CompareString(YW2File, "", TextCompare: false) == 0)
				{
					num2 = 5;
					YW2File = clsShared.cFileSel.GetFile(clsShared.cSpecialFolders.MyDocsFolder, "*.YWP");
				}
				num2 = 6;
				if (Operators.CompareString(YW2File, "", TextCompare: false) != 0)
				{
					num2 = 7;
					clsYW2Project.LoadProject(YW2File);
					num2 = 8;
					Interaction.MsgBox("You must create the new yWriter project in an EMPTY folder");
					num2 = 9;
					if (cProject.CreateEmptyProject(V6Project: false))
					{
						num2 = 10;
						cProject.Title = clsYW2Project.ProjectTitle;
						num2 = 11;
						cProject.AuthorName = clsYW2Project.AuthorName;
						num2 = 12;
						int Index1 = 1;
						do
						{
							num2 = 13;
							cProject.set_DeadlineDate(Index1, clsYW2Project.get_DeadlineDate(Index1));
							num2 = 14;
							cProject.set_DeadlineStartDate(Index1, clsYW2Project.get_DeadlineStartDate(Index1));
							num2 = 15;
							Index1++;
						}
						while (Index1 <= 5);
						num2 = 16;
						cProject.WordCountStart = clsYW2Project.WordCountStart;
						num2 = 17;
						cProject.WordTarget = clsYW2Project.WordTarget;
						num2 = 18;
						cProject.WordTargetEndDate = clsYW2Project.WordTargetEndDate;
						num2 = 19;
						cProject.WordTargetStartDate = clsYW2Project.WordTargetStartDate;
						num2 = 20;
						int chapterCount = clsYW2Project.ChapterCount;
						for (Index1 = 1; Index1 <= chapterCount; Index1++)
						{
							num2 = 21;
							clsChapter cChapter = new clsChapter();
							num2 = 22;
							cChapter.Title = clsYW2Project.get_Chapter(Index1).ChapterTitle;
							num2 = 23;
							cProject.AddChapter(ref cChapter);
							num2 = 24;
							int sceneCount = clsYW2Project.get_Chapter(Index1).SceneCount;
							for (int i = 1; i <= sceneCount; i++)
							{
								num2 = 25;
								clsScene Scene = new clsScene();
								num2 = 26;
								Scene.V6 = cProject.V6File;
								num2 = 27;
								Scene.Conflict = clsYW2Project.get_Chapter(Index1).get_Scene(i).Conflict;
								num2 = 28;
								Scene.Day_Renamed = clsYW2Project.get_Chapter(Index1).get_Scene(i).Day_Renamed;
								num2 = 29;
								Scene.DescText = clsYW2Project.get_Chapter(Index1).get_Scene(i).LongDesc;
								num2 = 30;
								((clsSceneBase)Scene).set_Field(1, clsYW2Project.get_Chapter(Index1).get_Scene(i).Field1);
								num2 = 31;
								((clsSceneBase)Scene).set_Field(2, clsYW2Project.get_Chapter(Index1).get_Scene(i).Field2);
								num2 = 32;
								((clsSceneBase)Scene).set_Field(3, clsYW2Project.get_Chapter(Index1).get_Scene(i).Field3);
								num2 = 33;
								((clsSceneBase)Scene).set_Field(4, clsYW2Project.get_Chapter(Index1).get_Scene(i).Field4);
								num2 = 34;
								Scene.Goal = clsYW2Project.get_Chapter(Index1).get_Scene(i).Goal;
								num2 = 35;
								Scene.Hour_Renamed = clsYW2Project.get_Chapter(Index1).get_Scene(i).Hour_Renamed;
								num2 = 36;
								Scene.LastsDays = clsYW2Project.get_Chapter(Index1).get_Scene(i).LastsDays;
								num2 = 37;
								Scene.LastsHours = clsYW2Project.get_Chapter(Index1).get_Scene(i).LastsHours;
								num2 = 38;
								Scene.LastsMinutes = clsYW2Project.get_Chapter(Index1).get_Scene(i).LastsMinutes;
								num2 = 39;
								Scene.Minute_Renamed = clsYW2Project.get_Chapter(Index1).get_Scene(i).Minute_Renamed;
								num2 = 40;
								Scene.Outcome = clsYW2Project.get_Chapter(Index1).get_Scene(i).Result;
								num2 = 41;
								Scene.Status = clsYW2Project.get_Chapter(Index1).get_Scene(i).Status;
								num2 = 42;
								Scene.TextRTF = clsYW2Project.get_Chapter(Index1).get_Scene(i).Content;
								num2 = 43;
								Scene.Title = clsYW2Project.get_Chapter(Index1).get_Scene(i).Desc;
								num2 = 44;
								string actor = clsYW2Project.get_Chapter(Index1).get_Scene(i).Actor;
								num2 = 45;
								int num4 = clsYW2Project.ActorNum(actor);
								num2 = 46;
								if (num4 > 0)
								{
									num2 = 47;
									bool flag = false;
									num2 = 48;
									IEnumerator enumerator = cProject.Chars.GetEnumerator();
									while (enumerator.MoveNext())
									{
										clsCharacter clsCharacter = (clsCharacter)enumerator.Current;
										num2 = 49;
										if (clsShared.cText.StringsEqual(clsCharacter.Title, actor))
										{
											num2 = 50;
											value = Conversions.ToString(clsCharacter.ID);
											num2 = 51;
											flag = true;
											break;
										}
										num2 = 53;
									}
									num2 = 54;
									if (enumerator is IDisposable)
									{
										(enumerator as IDisposable).Dispose();
									}
									num2 = 55;
									if (!flag)
									{
										num2 = 56;
										clsCharacter clsCharacter = new clsCharacter();
										num2 = 57;
										clsCharacter.Title = clsYW2Project.get_Actor(num4).ActorName;
										num2 = 58;
										clsCharacter.AKA = clsYW2Project.get_Actor(num4).AlternateNames;
										num2 = 59;
										clsCharacter.Bio = clsYW2Project.get_Actor(num4).Bio;
										num2 = 60;
										clsCharacter.FullName = clsYW2Project.get_Actor(num4).FullName;
										num2 = 61;
										clsCharacter.Goals = clsYW2Project.get_Actor(num4).Goals;
										num2 = 62;
										cProject.AddChar(ref clsCharacter);
										num2 = 63;
										value = Conversions.ToString(clsCharacter.ID);
									}
									num2 = 64;
									Scene.SetViewpointByID(Conversions.ToInteger(value));
								}
								num2 = 65;
								clsShared.cLog.AddAction("Added " + Scene.Title + " of " + Conversions.ToString(Scene.Wordcount) + " words");
								num2 = 66;
								cProject.AddSceneToProjectAndChapter(ref Scene, cChapter.ID);
								num2 = 67;
								Scene.SaveRTFV5(cProject.ProjectPath());
								num2 = 68;
							}
							num2 = 69;
						}
						num2 = 70;
						int actorCount = clsYW2Project.ActorCount;
						for (Index1 = 1; Index1 <= actorCount; Index1++)
						{
							num2 = 71;
							string actor = clsYW2Project.get_Actor(Index1).ActorName;
							num2 = 72;
							bool flag = false;
							num2 = 73;
							IEnumerator enumerator2 = cProject.Chars.GetEnumerator();
							while (enumerator2.MoveNext())
							{
								clsCharacter clsCharacter = (clsCharacter)enumerator2.Current;
								num2 = 74;
								if (clsShared.cText.StringsEqual(clsCharacter.Title, actor))
								{
									num2 = 75;
									flag = true;
									break;
								}
								num2 = 77;
							}
							num2 = 78;
							if (enumerator2 is IDisposable)
							{
								(enumerator2 as IDisposable).Dispose();
							}
							num2 = 79;
							if (!flag)
							{
								num2 = 80;
								clsCharacter clsCharacter = new clsCharacter();
								num2 = 81;
								clsCharacter.Title = clsYW2Project.get_Actor(Index1).ActorName;
								num2 = 82;
								clsCharacter.AKA = clsYW2Project.get_Actor(Index1).AlternateNames;
								num2 = 83;
								clsCharacter.Bio = clsYW2Project.get_Actor(Index1).Bio;
								num2 = 84;
								clsCharacter.FullName = clsYW2Project.get_Actor(Index1).FullName;
								num2 = 85;
								clsCharacter.Goals = clsYW2Project.get_Actor(Index1).Goals;
								num2 = 86;
								cProject.AddChar(ref clsCharacter);
								num2 = 87;
								clsShared.cLog.AddAction("Added " + clsCharacter.Title + " (Unused Character)");
							}
							num2 = 88;
						}
						num2 = 89;
						if (cProject.CriticalSave())
						{
							num2 = 90;
							result = true;
						}
					}
				}
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num5 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_0925: Could not find block for branch target IL_08f1*/;
			}
			if (num5 != 0)
			{
				ProjectData.ClearProjectError();
			}
			return result;
		}
	}
}
